package com.hepinghospital.user.ui.dialog;

import android.app.Dialog;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hepinghospital.user.R;
import com.hepinghospital.user.base.BaseDialogFragment;
import com.hepinghospital.user.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.response.Doctor;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import jz.joyoung.robot.util.DpUtil;


/**
 * Created by Administrator on 2017/11/23.
 */

public class SchudeleDialogFragment extends BaseDialogFragment {
    private Dialog mDialog;
    private OnItemClickListener mListener;
    private RecyclerView recyclerView;
    private MyAdapter adapter;
    private String docId, deptId, schDate, timeInterval;

    private List<Doctor.Schedule> list = new ArrayList<>();


    public static SchudeleDialogFragment newInstance(String docId) {
        Bundle args = new Bundle();
        args.putString( "docId", docId );
        SchudeleDialogFragment fragment = new SchudeleDialogFragment();
        fragment.setArguments( args );
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate( R.layout.dialog_datetime, container );
        recyclerView = view.findViewById( R.id.recyclerView );
        LinearLayoutManager manager = new LinearLayoutManager( getActivity() );
        recyclerView.setLayoutManager( manager );
        adapter = new MyAdapter();
        recyclerView.setAdapter( adapter );
        recyclerView.addItemDecoration( new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw( c, parent, state );
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets( outRect, view, parent, state );
                outRect.right = DpUtil.dip2px( getActivity(), 10 );
                outRect.left = DpUtil.dip2px( getActivity(), 10 );
                outRect.bottom = DpUtil.dip2px( getActivity(), 10 );
            }
        } );
        docId = getArguments().getString( "docId" );
        getDoctorDetail();
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );


    }

    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDialog = super.onCreateDialog( savedInstanceState );
        mDialog.setCancelable( false );
        Window window = mDialog.getWindow();
        if (window != null) {
            window.setGravity( Gravity.BOTTOM );
//            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        return mDialog;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
//        getDialog().getWindow().requestFeature( Window.FEATURE_NO_TITLE );
        super.onActivityCreated( savedInstanceState );
        getDialog().getWindow().setBackgroundDrawable( new ColorDrawable( 0x00000000 ) );
        getDialog().getWindow().setLayout( WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT );
    }

    public OnItemClickListener getmListener() {
        return mListener;
    }

    public void setmListener(OnItemClickListener mListener) {
        this.mListener = mListener;
    }


    public interface OnItemClickListener {

        void onConfirm(Doctor.Schedule schedule
        );
    }

    public boolean isShowing() {
        return mDialog != null && mDialog.isShowing();
    }


    class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {

        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            Holder myHolder;
            myHolder = new Holder( LayoutInflater.from( getActivity() ).inflate( R.layout.item_schedule, parent, false ) );
            return myHolder;
        }

        @Override
        public void onBindViewHolder(Holder holder, final int position) {
            holder.tv_scheduleTime.setText( list.get( position ).scheduleTime );
            holder.tv_dateTime.setText( list.get( position ).dateTime );
            holder.tv_regFee.setText( list.get( position ).deptName );
            if (list.get( position ).isSchedule == 0) {
                holder.btn_confirm.setText( "已约满" );
                holder.btn_confirm.setBackgroundResource( R.drawable.shape_grey );
                holder.btn_confirm.setTextColor( Color.parseColor( "#D5D5D5" ) );
            } else {
                holder.btn_confirm.setText( "预约" );
                holder.btn_confirm.setBackgroundResource( R.drawable.selector_button );
                holder.btn_confirm.setTextColor( Color.parseColor( "#FFFFFF" ) );
            }
            holder.btn_confirm.setOnClickListener( new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onConfirm( list.get( position ) );
                }
            } );
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class Holder extends RecyclerView.ViewHolder {

            TextView tv_scheduleTime, tv_dateTime, tv_regFee, btn_confirm;

            public Holder(View itemView) {
                super( itemView );
                tv_scheduleTime = itemView.findViewById( R.id.tv_scheduleTime );
                tv_dateTime = itemView.findViewById( R.id.tv_dateTime );
                tv_regFee = itemView.findViewById( R.id.tv_regFee );
                btn_confirm = itemView.findViewById( R.id.btn_confirm );
            }
        }
    }


    private void getDoctorDetail() {
        RestProxy.getInstance().getDoctorDetail( docId, new Observer<Doctor>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Doctor doctorDetailResponse) {
                list.clear();
                list.addAll( doctorDetailResponse.scheduleInfoDtoList );
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError( getActivity(), e );
            }

            @Override
            public void onComplete() {

            }
        } );


    }

}
